from tablestore import *
from django.conf import settings
class TableStore(object):
    """tablestore工具类"""
    def __init__(self):
        self.client = OTSClient(settings.OTS_ENDPOINT, settings.OTS_ID, settings.OTS_SECRET, settings.OTS_INSTANCE)

    def get_one(self,table_name, primary_key,columns_to_get = []):
        """根据主键获取一条数据"""
        try:
            consumed, return_row, next_token = self.client.get_row(table_name, primary_key, columns_to_get)
        except:
            return {}
        if return_row is None:
            return {}
        else:
            result = return_row.primary_key + return_row.attribute_columns
            data = {}
            for item in result:
                data[item[0]] = item[1]  # user_id:数值
            return data